//
//  AddVC.m
//  AddressBook
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AddVC.h"
#import <Masonry.h>
#import "TextField.h"
#import "CoreData.h"
#import "Person.h"

@interface AddVC ()

@property(nonatomic, strong) CoreData *manager;

@end

@implementation AddVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // name
    TextField *nameTextField = [[TextField alloc] init];
    [self.view addSubview:nameTextField];
    nameTextField.textField.placeholder = @"请输入姓名";
    nameTextField.tag = 1000;
    
    [nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(84);
        make.centerX.equalTo(self.view.mas_centerX).with.offset(0);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 2);
    }];
    
    // telephone
    TextField *telephoneTextField = [[TextField alloc] init];
    [self.view addSubview:telephoneTextField];
    telephoneTextField.textField.placeholder = @"请输入电话";
    telephoneTextField.tag = 1001;
    
    [telephoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameTextField.mas_bottom).with.offset(20);
        make.centerX.equalTo(self.view.mas_centerX).with.offset(0);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 2);
    }];
    
    // sex
    TextField *sexTextField = [[TextField alloc] init];
    [self.view addSubview:sexTextField];
    sexTextField.textField.placeholder = @"请输入性别";
    sexTextField.tag = 1002;
    
    [sexTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(telephoneTextField.mas_bottom).with.offset(20);
        make.centerX.equalTo(self.view.mas_centerX).with.offset(0);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 2);
    }];
    
    // number
    TextField *numberTextField = [[TextField alloc] init];
    [self.view addSubview:numberTextField];
    numberTextField.textField.placeholder = @"请输入学号";
    numberTextField.tag = 1003;
    
    [numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(sexTextField.mas_bottom).with.offset(20);
        make.centerX.equalTo(self.view.mas_centerX).with.offset(0);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 2);
    }];
    
    // QQ
    TextField *QQTextField = [[TextField alloc] init];
    [self.view addSubview:QQTextField];
    QQTextField.textField.placeholder = @"请输入QQ";
    QQTextField.tag = 1004;
    
    [QQTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(numberTextField.mas_bottom).with.offset(20);
        make.centerX.equalTo(self.view.mas_centerX).with.offset(0);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 2);
    }];
    
    // jiguan
    TextField *birthTextField = [[TextField alloc] init];
    [self.view addSubview:birthTextField];
    birthTextField.textField.placeholder = @"请输入籍贯";
    birthTextField.tag = 1005;
    [birthTextField.textField addTarget:self action:@selector(birthTextFieldUp:) forControlEvents:UIControlEventEditingDidBegin];
    [birthTextField.textField addTarget:self action:@selector(birthTextFieldDown:) forControlEvents:UIControlEventEditingDidEnd];
    
    [birthTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(QQTextField.mas_bottom).with.offset(20);
        make.centerX.equalTo(self.view.mas_centerX).with.offset(0);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 2);
    }];
    
    
    // button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor greenColor];
    [button setTitle:@"确认" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(birthTextField.mas_bottom).with.offset(40);
        make.centerX.equalTo(self.view.mas_centerX).with.offset(0);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width / 4);
    }];
    
    self.manager = [CoreData defaults];
    
     NSLog(@"url = %@", [self.manager applicationDocumentsDirectory]);
}



#pragma mark - block
- (void)button:(UIButton *)button {
    
    // save
    [self saveCoreData];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)saveCoreData {
    // 创建实例描述类
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.manager.managedObjectContext];
    
    // 创建实体
    Person *Add = [[Person alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    
    // 赋值
    TextField *nameTextField = (TextField *)[self.view viewWithTag:1000];
    TextField *telephoneTextField = (TextField *)[self.view viewWithTag:1001];
    TextField *sexTextField = (TextField *)[self.view viewWithTag:1002];
    TextField *numberTextField = (TextField *)[self.view viewWithTag:1003];
    TextField *QQTextField = (TextField *)[self.view viewWithTag:1004];
    TextField *birthTextField = (TextField *)[self.view viewWithTag:1005];
    
    Add.name = nameTextField.textField.text;
    Add.telephone = telephoneTextField.textField.text;
    Add.sex = sexTextField.textField.text;
    Add.number = numberTextField.textField.text;
    Add.qq = QQTextField.textField.text;
    Add.birth = birthTextField.textField.text;
    
    [self.manager saveContext];
    
}




#pragma mark - birthTextFieldAction
- (void)birthTextFieldUp:(UITextField *)textField {
    self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, -50);
}

- (void)birthTextFieldDown:(UITextField *)textField {
    self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, 50);
}



#pragma mark - touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    TextField *nameTextField = (TextField *)[self.view viewWithTag:1000];
    TextField *telephoneTextField = (TextField *)[self.view viewWithTag:1001];
    TextField *sexTextField = (TextField *)[self.view viewWithTag:1002];
    TextField *numberTextField = (TextField *)[self.view viewWithTag:1003];
    TextField *QQTextField = (TextField *)[self.view viewWithTag:1004];
    TextField *birthTextField = (TextField *)[self.view viewWithTag:1005];
    
    [nameTextField.textField resignFirstResponder];
    [telephoneTextField.textField resignFirstResponder];
    [sexTextField.textField resignFirstResponder];
    [numberTextField.textField resignFirstResponder];
    [QQTextField.textField resignFirstResponder];
    [birthTextField.textField resignFirstResponder];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
